import { Link } from '@brillout/docpress'


## Server-side

Server-side errors are exposed over `pageContext.errorWhileRendering` at <Link href="/renderPage">`renderPage()`</Link>:

```js
// server.js

import { renderPage } from 'vike/server'

// Any server: Express.js, Cloudflare Worker, AWS Lambda Function, Fastify, Hono, Nitro, ...
server.addMiddleware({
  method: 'GET',
  route: '*', // catch-all
  async handler(request) {
    const pageContext = await renderPage({ urlOriginal: request.url })

    if (pageContext.errorWhileRendering) {
      /* Vike already calls console.error() so the following line isn't needed.
      console.error(pageContext.errorWhileRendering)
      */

      // If you use an error tracker (e.g. Sentry):
      myErrorTracker.intercept(pageContext.errorWhileRendering)

      // If you use a server middleware that handles errors:
      if (!pageContext.httpResponse) {
        // Don't throw if there is a pageContext.httpResponse, otherwise
        // the error page won't be rendered.
        throw pageContext.errorWhileRendering
      }

    }

    // pageContext.httpResponse is missing if:
    //  - There is an error and you didn't define an error page.
    //  - Your error page has a bug and, therefore, couldn't be rendered.
    if (!pageContext.httpResponse) return null

    // The HTTP response of the page. This can be the error page.
    return pageContext.httpResponse
  }
})
```

> Vike calls `console.error(err)` for any error it encounters. With the upcoming [new hook `onLog()` (#1439)](https://github.com/vikejs/vike/issues/1438) you'll have control over Vike's logging.

See also <Link href="/error-page" />.

You can use any error tracker, such as [Sentry](https://sentry.io/), [Bugsnag](https://www.bugsnag.com/), [Rollbar](https://rollbar.com/), or your own custom error tracking. For that, make sure to use the error tracker's server-side interceptor. For example with Sentry:

```js
import * as Sentry from "@sentry/node";

// ...

    if (pageContext.errorWhileRendering) {
      Sentry.captureException(e)
    }

// ...
```


## Client-side

Use <Link href="/client">`+client.js`</Link> in order to make sure that your error handling code is initialized before any other code executes.

```js
// /pages/+client.js

// The first lines of JavaScript executed on the website.

window.addEventListener('error', (err) => {
   console.error('An error occured:', err)
})
```


## Verbose

Vike prettifies transpilation errors, such as errors thrown by [esbuild](https://esbuild.github.io/) and [Babel](https://babeljs.io/).

While Vike is careful about not removing relevant information, it may mistakenly do so. In that case [create a new GitHub issue](https://github.com/vikejs/vike/issues/new). Until a Vike maintainer fixes the issue you can use the debug flag `DEBUG=vike:error`.

```shell
# Show verbose original errors, with infinite stack trace (`Error.stackTraceLimit = Infinity`)
DEBUG=vike:error npm run dev
```

For even more information:

```shell
# Usually only used by Vike maintainers
DEBUG=vike:error,vike:log npm run dev
```


## See also

 - <Link href="/renderPage" />
 - <Link href="/error-page" />
 - <Link href="/client" />
 - <Link href="/pageContext" />
